package jmcnet.libcommun.logger
{
	import flash.utils.getQualifiedClassName;
	
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class JMCNetLog4JLogger extends JMCNetLogger
	{
		private var _logger:String=null;
		public function JMCNetLog4JLogger(logger:String) {
			this._logger = logger;
		}
		
		public static function getLogger(classe:Object):JMCNetLog4JLogger {
			return new JMCNetLog4JLogger(flash.utils.getQualifiedClassName(classe));
		}
		
		
		public function debug(message:String):void{
				logWithLogger(LEVEL_DEBUG, _logger, message);
		}
		
		public function info(message:String):void{
				logWithLogger(LEVEL_INFO, _logger, message);
		}
		
		public function evt(message:String):void{
				logWithLogger(LEVEL_EVT, _logger, message);
		}
		
		public function warn(message:String):void{
				logWithLogger(LEVEL_WARN, _logger, message);
		}
		
		public function error(message:String):void{
				logWithLogger(LEVEL_ERROR, _logger, message);
		}
		
		public function logWithLogger(levelMessage:int, logger:String, message:String):void{
			
			if(! logEnabled) return;
			if (remoteLogEnabled && getRemoteLogger() != null) {
				// getRemoteLoggerObject().logMessageWithLogger(levelMessage, logger, message);
				var args:Array = [ levelMessage, logger, message];
				
				var operation:AbstractOperation = getRemoteLogger().getOperation("logMessageWithLogger");
				operation.arguments = args;
				var token:AsyncToken = operation.send();
				operation.addEventListener(FaultEvent.FAULT, onRemoteError);
				operation.addEventListener(ResultEvent.RESULT, onRemoteSuccess);
			}
			
			JMCNetLogger.log(levelMessage, logger+" "+message);
		}
		
		private function onRemoteError(event:FaultEvent):void {
			JMCNetLogger.warn("Attention : appel de RemoteLoggerObject avec erreur : evt="+event.toString());
		}
		
		private function onRemoteSuccess(event:ResultEvent):void {
			// JMCNetLogger.debug("appel de RemoteLoggerObject avec succes : evt="+event.toString());
		}
	}
}